package com.example.testrecorder;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.testrecorder.domain.QueAndAns;
import com.example.testrecorder.utils.LogUtil;
import com.example.testrecorder.utils.MyDialog;

import org.litepal.LitePal;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener{
    private Toolbar toolbar;
    private RelativeLayout relativeLayout,userRl,qaRl;
    private static final String TAG = "SettingActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
    }

    private void initView() {
        qaRl=findViewById(R.id.qaRL);
        qaRl.setOnClickListener(this);
        userRl=findViewById(R.id.userRL);
        userRl.setOnClickListener(this);
        toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        relativeLayout=findViewById(R.id.clearRL);
        relativeLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.clearRL:
                MyDialog myDialog=new MyDialog(SettingActivity.this, Color.parseColor("#f4f8fb"));
                myDialog.setTitle("警告");
                myDialog.setTitleTextColor("#F17271");
                myDialog.setContent("您确定要清空密保表吗？");
                myDialog.show();
                myDialog.setOnPositiveListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int delete = LitePal.deleteAll(QueAndAns.class);
                        LogUtil.logD(TAG,"成功删除："+delete+"条密保记录！");
                        myDialog.dismiss();
                        Toast.makeText(SettingActivity.this,"成功清除"+delete+"条数据！",Toast.LENGTH_SHORT).show();
                    }
                });
                myDialog.setOnNegativeListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myDialog.dismiss();
                    }
                });
                break;
            case R.id.userRL:
                Intent intent=new Intent(SettingActivity.this,ViewTableActivity.class);
                intent.putExtra("pa","userTabel");
                startActivity(intent);
                break;
            case R.id.qaRL:
                Intent intent2=new Intent(SettingActivity.this,ViewTableActivity.class);
                intent2.putExtra("pa","qaTabel");
                startActivity(intent2);
                break;
            default:
        }
    }
}